package com.gitee.ed.arithmetic.leetcode.answer.answer_1_100.answer_1_10;

import com.gitee.ed.arithmetic.leetcode.solution.solution_1_100.solution_1_10.Solution9;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hongda.li
 */
public class Answer9 implements Solution9 {
    @Override
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }

        if (x == 0) {
            return true;
        }

        List<Integer> list = new ArrayList<>();

        int temp = 10;
        int result = x;
        do {
            list.add(result % temp);
            result = result / temp;
        } while (result != 0);

        boolean allZero = true;

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == 0 && allZero) {
                continue;
            } else {
                allZero = false;
            }

            if (!Objects.equals(list.get(i), list.get(list.size() - i - 1))) {
                return false;
            }

            if (i > list.size() / 2 + 1) {
                return true;
            }
        }

        return true;
    }
}
